<?php namespace app\admin\controller;

use houdunwang\db\Db;
use houdunwang\page\Page;
use houdunwang\request\Request;
use system\model\Article as ArticleModel ;
use system\model\Category;
use module\Wechat;
use system\model\Keywords;

class Article  extends  Common  {
    use Wechat;
    public function __construct()
    {

        $this->auth();
    }

    //动作
    public function index(ArticleModel $article){
        $num = is_null(v( 'config.article_num' )) ? 10 : v( 'config.article_num' );
        $field = $article->paginate( $num );
        //此处书写代码...
//框架写法
//        $field = $article->paginate(v('config.article_num'));
//        原声写法

//        $page =Page::row(1)->make(Db::table('article')->count());
////        $page = Page::row(1)->make(Db::table('article')->count());
//  p($page);
//        $field =Db::table('article')->limit(Page::limit()->get());
//        $field = Db::table('article')->limit(Page::limit())->get();
        return view('',compact('field'));

    }


    public  function  post(ArticleModel $article ,Category $category){
        $arc_id=Request::get('arc_id',0,'intval');
        if(IS_POST){

            if($arc_id){
                $article  = ArticleModel::find($arc_id);
            }

//            获取post数据提交
            $post=Request::post();
                $article->save(Request::post());
//            p(Request::post());die;
            //添加关键词表数据
//            通过关键词找到文章id
            $arc_id = \system\model\Article::where('arc_keyword',$post['arc_keyword'])->pluck('arc_id');
//            将关键词 对应的id和对应得文章标题存到对应的字段
            $data = ['keywords'=>$post['arc_keyword'],'module_id'=>$arc_id,'module'=>'article'];
//            将data数据存入
            $this->saveKeywords($data);
//            提示消息
            $this->setRedirect('index')->success('操作成功');
        }
        if($arc_id){
            //编辑
            $oldData = ArticleModel::find($arc_id);
            //p($oldData->toArray());

        }

//        获取所属栏目
        $cateData = $category->getAllCate();
        return view('',compact('cateData','oldData'));
    }
    //删除
    public function remove(ArticleModel $article)
    {
        $arc_id = Request::get('arc_id');
        //执行删除
        $model = ArticleModel::find($arc_id);

        (new Keywords())::where('module_id',$arc_id)->first()->destory();
        $model->destory();
        //跳转
        go('index');
    }


}
